﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using A2DFramework.MVCFramework.Descriptors;
using A2DFramework.MVCFramework.Helpers;

namespace A2DFramework.MVCFramework.Parsers
{
    static class ParameterParser
    {
        public static object GetValue(HttpContext context, ParameterDescriptor pd)
        {
            object value=null;

            if (pd.SourceLocation == HttpAttributes.SourceLocation.Any)
            {
                value = TypeCastHelper.Cast(context.Request.Params[pd.KeyInSource], pd.ParameterType);
            }
            else if (pd.SourceLocation == HttpAttributes.SourceLocation.Cookies)
            {
                value = TypeCastHelper.Cast(context.Request.Cookies[pd.KeyInSource].Value, pd.ParameterType);
            }
            else if (pd.SourceLocation == HttpAttributes.SourceLocation.From)
            {
                value = TypeCastHelper.Cast(context.Request.Form[pd.KeyInSource], pd.ParameterType);
            }
            else if (pd.SourceLocation == HttpAttributes.SourceLocation.Headers)
            {
                value = TypeCastHelper.Cast(context.Request.Headers[pd.KeyInSource], pd.ParameterType);
            }
            else if (pd.SourceLocation == HttpAttributes.SourceLocation.QueryString)
            {
                value = TypeCastHelper.Cast(context.Request.QueryString[pd.KeyInSource], pd.ParameterType);
            }
            return value;
        }
    }
}
